package gogogo.liwushuo.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;

import java.util.List;

import gogogo.liwushuo.R;
import gogogo.liwushuo.bean.CommentBean;
import gogogo.liwushuo.constant.Resource;

/**
 * Created by chenjun on 2016/7/19.
 */
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyHolder> {

    private List<CommentBean.DataBean.ItemsBean> itemsBeen;
    private Context mContext;

    public RecyclerViewAdapter(Context context, List<CommentBean.DataBean.ItemsBean> itemsBeen) {
        mContext = context;
        this.itemsBeen = itemsBeen;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_home_recycler, parent, false);
        return new MyHolder(view);
    }

    public void setList(List<CommentBean.DataBean.ItemsBean> itemsBeen){
        this.itemsBeen = itemsBeen;
        notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
        CommentBean.DataBean.ItemsBean bean = itemsBeen.get(position);
        holder.tvCategory.setText(bean.getColumn().getCategory());
        holder.tvDigest.setText(bean.getTitle());
        holder.tvLikecCount.setText(bean.getLikes_count()+"");
        holder.tvName.setText(bean.getAuthor().getNickname());
        holder.tvTitle.setText(bean.getColumn().getTitle());
        String cover_image_url = bean.getCover_image_url();
        Picasso.with(mContext)
                .load(cover_image_url)
                .tag(cover_image_url)
                .placeholder(R.color.textcolor_unchecked)
                .error(R.color.textcolor_unchecked)
                .into(holder.ivImage);
        Picasso.with(mContext)
                .load(bean.getAuthor().getAvatar_url())
                .tag(cover_image_url)
                .placeholder(R.color.textcolor_unchecked)
                .error(R.color.textcolor_unchecked)
                .into(holder.ivIcon);
    }

    @Override
    public int getItemCount() {
        return itemsBeen == null ? 0 : itemsBeen.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        private TextView tvCategory, tvTitle, tvName, tvDigest, tvLikecCount;
        private ImageView ivIcon, ivImage;

        public MyHolder(View v) {
            super(v);
            tvCategory = (TextView) v.findViewById(R.id.tv_category_recycler);
            tvTitle = (TextView) v.findViewById(R.id.tv_title_recycler);
            tvName = (TextView) v.findViewById(R.id.tv_author_recycler);
            tvDigest = (TextView) v.findViewById(R.id.tv_digest_recycler);
            tvLikecCount = (TextView) v.findViewById(R.id.tv_count_recycler);
            ivIcon = (ImageView) v.findViewById(R.id.iv_icon_recycler);
            ivImage = (ImageView) v.findViewById(R.id.iv_image_recycler);
        }
    }
}
